var chord_delay_view;
var currentAmount = 0;
$(document).ready(function () {
    $('#chord_view').draggable();
    $('#chord_view .closebutton').click(function () {
        $('#chord_view').slideUp();
    })
    $('span.hocdan_chord').mouseover(function () {
        var obj = $(this);
        clearTimeout(chord_delay_view);
        chord_delay_view = setTimeout(function () {
            var pos = obj.offset();
            var posCont = $('#lyric_content').offset();
            $('#draw_chord').html('<p>Loading....</p>');
            $('#chord_view').css({
                position: "absolute",
                top: (pos.top - posCont.top - 180) + "px",
                left: (pos.left - posCont.left - 70) + "px"
            }).slideDown(function () {
                    $('#chord_view #draw_chord').attr('value', obj.html());
                    jtab.render($('#chord_view #draw_chord'), obj.html());
                });
        }, 2);
    }).mouseleave(function () {
            clearTimeout(chord_delay_view);
        });

    $('#disp_ch_highlight').bind('click', function () {
        if (document.getElementById('disp_ch_highlight').checked == true) {
            $('#cont span').css({
                'color': '#D07035',
                'cursor': 'pointer'
            });
//            bind_hover();
        }
        else {
            $('#cont span').css({
                'color': '#000',
                'cursor': 'auto'
            });
            $('#cont span').unbind('mouseover');
        }
    });
    $('#disp_speard').bind('click', function () {
        if (document.getElementById('disp_speard').checked == true) {
            $('.speard').css({
                'width': '49.4%'
            });
            $('.speard:first').css({
                'margin-right': '1%',
                'border-right': "dashed 1px"
            })
        } else {
            $('.speard').css({
                'width': '100%'
            });
            $('.speard:first').css({
                'margin-right': 'none',
                'border-right': "none"
            })
        }
    })
    $('#disp_ch_diagrams').bind('click', function () {
        if (document.getElementById('disp_ch_diagrams').checked == true) {
            $('#cont span').show();
//            $('#chords_list').fadeIn(500);
        }
        else {
            $('#cont span').hide();
//            $('#chords_list').hide();
        }
    });
    $('#tone-up').click(function () {
        changeAllChord(1);
        $('#transpose_val').show();
    });
    $('#tone-down').click(function () {
        changeAllChord(-1);
        $('#transpose_val').show();
    });
    $('#main-tone').click(function () {
        changeAllChord(-currentAmount);
        currentAmount = 0;
        $('#transpose_val').hide();
    });
});

function changeAllChord(amount) {
    $('#cont span.hocdan_chord').each(function () {
        var obj = $(this);
        obj.html(jtab.transposeChord(obj.html(), amount));
    })
    $('#main-tone').html(jtab.transposeChord($('#main-tone').html(), amount));
    currentAmount += amount;
    if (currentAmount == 12)
        currentAmount = 0;
    if (currentAmount == -1)
        currentAmount = 11;
    $('.chord_inline').each(function () {
        $(this).removeClass('selected');
    })
    $('.chord_inline[value="' + currentAmount + '"]').addClass('selected');

    $('#chord_view #draw_chord').each(function () {
        $(this).attr('value', jtab.transposeChord($(this).attr('value'), amount));
    });
    refreshUsedChord();
}

function refreshUsedChord() {
    $('.sidebarchord').each(function () {
        var obj = $(this);
        jtab.render(obj, obj.attr("value"));
    });
    jtab.render($('#chord_view #draw_chord'), $('#chord_view #draw_chord').attr('value'));
}

function listChord(chords) {
    for (c in chords) {

    }
}

function startScroll() {
    window.scrollBy(1, 1);
    if ($(window).scrollTop() > $('#lyric_content').offset().top + $('#lyric_content').height - $(window).height() / 4) {
        if ($('#scroll_repeat').is(':checked')) {
            $('body').goTo();
            scrolldelay = setTimeout('startScroll()', 5000);
        } else
            stopScroll();
    } else {
        scrolldelay = setTimeout('startScroll()', 200 / scrollValue);
    }
}

function stopScroll() {
    scrollValue = 0;
    $('#scroll_panel').css('opacity', 1);
    clearTimeout(scrolldelay);
}

function pageScroll() {
    $('#scroll_panel').css('opacity', 0.5);
    startScroll();
}
function refreshColors() {
    $('.hover').each(function () {
        if (parseInt($(this).attr('value')) <= scrollValue) {
            $(this).addClass('color');
        } else {
            $(this).removeClass('color');
        }
    });
}

var settings = {
    fontsize: 15
}
var scrollValue = 0;
var scrolldelay;
$(document).ready(function () {
    if (typeof settings.fontsize != 'undefined') {
        $('#lyric_content').css('font-size', settings.fontsize + 'px');
    }
    $('#size_big').click(function () {
        var newSize = (parseInt($('#cont').css('font-size')) + 1);
        $('#cont').css('font-size', newSize + 'px');
        settings.fontsize = newSize;
//        saveSettings();
//        setSongContentSize();
    });
    $('#size_small').click(function () {
        var newSize = (parseInt($('#cont').css('font-size')) - 1);
        $('#cont').css('font-size', newSize + 'px');
        settings.fontsize = newSize;
//        saveSettings();
//        setSongContentSize();
    });
//    $('#default_fontsize').click(function () {
//        var newSize = 15;
//        $('#song_text').css('font-size', newSize + 'px');
//        settings.fontsize = newSize;
//        saveSettings();
//        setSongContentSize();
//    });
    $('#i_scroll').click(function () {
        $('#scroll_panel').toggleClass('hide');
        if ($('#scroll_panel').hasClass('hide')) {
            stopScroll();
        } else {
            $('#lyric_content').focus();
            refreshColors();
            $('#scroll_panel').css('left', $('#lyric_content').offset().right);
            $('#scroll_panel').css('top', 40);
        }
        $('#scroll_panel').draggable();
    });

    $('#scroll_panel .closebutton span').click(function () {
        $('#i_scroll').click();
    });

    $('#music_embed object').css({position: "relative", bottom: "249px"});
    $('#music_embed iframe').css({position: "relative", bottom: "63px"})
    $('.hover').mouseover(function () {
        var obj = $(this);
        $('.hover').each(function () {
            if (parseInt($(this).attr('value')) <= obj.attr('value')) {
                $(this).addClass('color');
            } else {
                $(this).removeClass('color');
            }
        });
    }).mouseleave(function () {
            refreshColors();
        }).click(function () {
            scrollValue = parseInt($(this).attr('value'));
            //console.log($(this).attr('value'));
            if (scrollValue == 0) {
                stopScroll();
            } else {
                clearTimeout(scrolldelay);
                pageScroll();
            }
        });
    $(document).keydown(function (e) {
        if (e.which == 27 && scrollValue != 0) {
            stopScroll();
            $('.hover').each(function () {
                if (parseInt($(this).attr('value')) == 0) {
                    $(this).addClass('color');
                } else {
                    $(this).removeClass('color');
                }
            });
        }
    });
    var duplicateChk = {};
    refreshUsedChord();
    $('#box_chords .sidebarchord').each(function () {
        var obj = $(this);
        if (duplicateChk.hasOwnProperty(obj.attr('value'))) {
            $(this).remove();
        } else {
            duplicateChk[obj.attr('value')] = 'true';
        }
    });
    var duplicateChk = {};
    $('#hide_slide_chord').click(function () {
        $('#slide_chord').toggle("slow");
    });
});